package cn.zhiup.mobile.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import android.widget.Toast;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import cn.zhiup.mobile.Constant;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler
{
	private IWXAPI api;
	public static final String wxPayAction = "wx_pay_success";
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		api = WXAPIFactory.createWXAPI(this, Constant.WX_APP_ID);
		api.handleIntent(getIntent(), this);
	}

	@Override
	protected void onNewIntent(Intent intent)
	{
		super.onNewIntent(intent);
		setIntent(intent);
		api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq arg0) {}

	@Override
	public void onResp(BaseResp arg0) {
		if (arg0.getType() == ConstantsAPI.COMMAND_PAY_BY_WX)
		{
			if(arg0.errCode == 0){
				Intent intent = new Intent();
				intent.setAction(wxPayAction);
				sendBroadcast(intent);
				Toast.makeText(this,"微信支付成功",Toast.LENGTH_LONG).show();
			}else if(arg0.errCode == -1){
				Toast.makeText(this,"微信支付失败"+arg0.errStr+arg0.transaction+arg0.openId,Toast.LENGTH_LONG).show();
			}else if(arg0.errCode == -2){
				Toast.makeText(this,"微信支付取消",Toast.LENGTH_LONG).show();
			}
		}
//		WXPayEntryActivity.this.finish();
	}
}